package com.dkd.manage.controller;
import java.time.Duration;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.StrUtil;
import com.dkd.common.constant.DkdContants;
import com.dkd.manage.domain.TbTaskDetails;
import com.dkd.manage.domain.dto.TaskDto;
import com.dkd.manage.service.impl.TbTaskDetailsServiceImpl;
import com.dkd.manage.service.impl.TbTaskServiceImpl;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dkd.common.annotation.Log;
import com.dkd.common.core.controller.BaseController;
import com.dkd.common.core.domain.AjaxResult;
import com.dkd.common.enums.BusinessType;
import com.dkd.manage.domain.TbTask;
import com.dkd.manage.service.ITbTaskService;
import com.dkd.common.utils.poi.ExcelUtil;
import com.dkd.common.core.page.TableDataInfo;
/**
 * 运营工单Controller
 *
 * @author ruoyi
 * @date 2024-12-02
 */
@RestController
@RequestMapping("/manage/task")
public class TbTaskController extends BaseController {
    @Autowired
    private ITbTaskService tbTaskService;
    @Autowired
    private TbTaskServiceImpl tbTaskServiceImpl;
    @Qualifier("redisTemplate")
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TbTaskDetailsServiceImpl tbTaskDetailsServiceImpl;
    /**
     * 查询运营工单列表
     */
    @PreAuthorize("@ss.hasPermi('manage:task:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbTask tbTask) {
        startPage();
        List<TbTask> list = tbTaskService.selectTbTaskList(tbTask);
        return getDataTable(list);
    }
    /**
     * 导出运营工单列表
     */
    @PreAuthorize("@ss.hasPermi('manage:task:export')")
    @Log(title = "运营工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbTask tbTask) {
        List<TbTask> list = tbTaskService.selectTbTaskList(tbTask);
        ExcelUtil<TbTask> util = new ExcelUtil<TbTask>(TbTask.class);
        util.exportExcel(response, list, "运营工单数据");
    }
    /**
     * 获取运营工单详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:task:query')")
    @GetMapping(value = "/{taskId}")
    public AjaxResult getInfo(@PathVariable("taskId") Long taskId) {
        return success(tbTaskService.selectTbTaskByTaskId(taskId));
    }
    /**
     * 新增运营工单
     */
    @PreAuthorize("@ss.hasPermi('manage:task:add')")
    @Log(title = "运营工单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TaskDto taskDto) {
        Integer result = tbTaskServiceImpl.addTaskDto(taskDto);
        return toAjax(result);
    }
    //生成并获取当天工单编号（唯一编号）
    public String generateTaskCode() {
        //当天日期以YYYYMMDD的格式
        LocalDate now = LocalDate.now();
        String formatDate = now.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        //根据日期，生成radis的键
        String key = "task_id:" + formatDate;
        //最终的taskId
        String taskCode = null;
        //生成4位的自增数
        String padPre;
        //判断key是否存在
        if (!redisTemplate.hasKey(key)) {
            //如果不存在，则设置key键，并设置初始值为1
            redisTemplate.opsForValue().set(key, 1, Duration.ofDays(1));
            //生成4位的自增数，前面补0，确保4位数
            padPre = StrUtil.padPre("1", 4, "0");
        } else {
            //自增1
            String incStr = redisTemplate.opsForValue().increment(key).toString();
            //不足4位，在前面补0
            padPre = StrUtil.padPre(incStr, 4, "0");
        }
        taskCode = formatDate + padPre;
        return taskCode;
    }
    /**
     * 修改运营工单
     */
    @PreAuthorize("@ss.hasPermi('manage:task:edit')")
    @Log(title = "运营工单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbTask tbTask) {
        return toAjax(tbTaskService.updateTbTask(tbTask));
    }
    /**
     * 删除运营工单
     */
    @PreAuthorize("@ss.hasPermi('manage:task:remove')")
    @Log(title = "运营工单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{taskIds}")
    public AjaxResult remove(@PathVariable Long[] taskIds) {
        return toAjax(tbTaskService.deleteTbTaskByTaskIds(taskIds));
    }
}
